package com.fanrenqiji.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Dadou on 2017/4/28.
 */
public class RegexUtils {
    /**
     * 获取指定HTML标签的指定属性的值
     *
     * @param source
     *            要匹配的源文本
     * @param element
     *            标签名称
     * @param attr
     *            标签的属性名称
     * @param flag
     *            是否为双标签
     * @return 属性值列表
     */

    /**
     * 获取指定HTML标签的指定属性的值
     *
     * @param source
     *            要匹配的源文本
     * @param element
     *            标签名称
     * @param attr
     *            标签的属性名称,如果属性值为"" 或 NULL 匹配整个标签
     * @return 属性值列表
     */
    public static List<String> match(String source, String element, String attr, boolean flag) {
        List<String> result = new ArrayList<String>();
        String reg;
        // 取整个标签
        if (StringUtils.isEmpty(attr)) {

            // 是否为双标签
            if (flag) {
                reg = "<" + element + "(\\s.*?)?</" + element + ">";
                Matcher m = Pattern.compile(reg).matcher(source);
                while (m.find()) {
                    String r = m.group();
                    result.add(r);
                }

            } else {
                reg = "<" + element + "(\\s.*?)?/>";
                Matcher m = Pattern.compile(reg).matcher(source);
                while (m.find()) {
                    String r = m.group();
                    result.add(r);
                }
            }
        } else {
            reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>";
            Matcher m = Pattern.compile(reg).matcher(source);
            while (m.find()) {
                String r = m.group(1);
                result.add(r);
            }
        }
        return result;
    }

   public static void main(String[] args) {
        String s = "<p>图片</p><p><img data-s=\\\"300,640\\\" data-type=\\\"png\\\" data-src=\\\"http://mmbiz.qpic.cn/mmbiz_png/QShL3QH1VIanyEzNScrS2Piaql7PgkJw1Ym8eHKwXmTw6BVRMVpbrOIHD9wg9SewkAdyNAtjybkDeKNR5nxugUQ/0?wx_fmt=png\\\" data-ratio=\\\"1.328358208955224\\\" data-w=\\\"134\\\"  />fdfdfdfdf<br  />视频<iframe class=\\\"video_iframe\\\" data-vidtype=\\\"2\\\" style=\\\"   z-index:1; \\\" height=\\\"375\\\" width=\\\"500\\\" frameborder=\\\"0\\\" data-src=\\\"https://v.qq.com/iframe/preview.html?vid=f13094knfks&amp;width=500&amp;height=375&amp;auto=0\\\" allowfullscreen=\\\"\\\"></iframe><iframe class=\\\"video_iframe\\\" data-vidtype=\\\"2\\\" style=\\\"   z-index:1; \\\" height=\\\"375\\\" width=\\\"500\\\" frameborder=\\\"0\\\" data-src=\\\"https://v.qq.com/iframe/preview.html?vid=f13094knfks&amp;width=500&amp;height=375&amp;auto=0\\\" allowfullscreen=\\\"\\\"></iframe>fdfdfdf<br  />音频</p><p><mpvoice frameborder=\\\"0\\\" class=\\\"res_iframe js_editor_audio audio_iframe\\\" src=\\\"/cgi-bin/readtemplate?t=tmpl/audio_tmpl&amp;name=1474265944824_461u99.mp3&amp;play_length=00:56\\\" name=\\\"1474265944824_461u99.mp3\\\" play_length=\\\"56000\\\" voice_encode_fileid=\\\"MzA5NTk5MTc4MV81MDQwMzU4NTE=\\\"></mpvoice></p><p><br  /></p><p><br  /></p>\",\n";
        String a="回家的姐夫你能否难道你你你就觉得没你想你想你成年男性南京参加<img src=http://data.fanrenqiji.com/upload/20170427/b8c8fdbf-3e51-4ac7-a8f9-0a88bc4be685.JPG/><img src=http://data.fanrenqiji.com/upload/20170427/38bb000b-9c48-4690-a168-ea871ab1d5b4.PNG/><img src=http://data.fanrenqiji.com/upload/20170427/650d50c0-ed60-473e-9a1b-410050028d74.PNG/>";
       /*List<String> iframe = match(s, "iframe", "data-src", true);*/
        List<String> match = match(a, "img", "src", false);
        System.out.println(match.size());
        for (String ss : match) {
            System.out.println(ss);
        }
        System.out.print(toSummary(""));
        System.out.print("\"");

    }
    public static String toSummary(String text) {
        //text="光哥说要一个<span style=\\\"font-size:24px;\\\"><strong>复杂</strong></span>点的富文本，然后我就添了一张图片<br />\\r\\n<img alt=\\\"这是一张图片\\\" src=\\\"http://data.fanrenqiji.com/upload/20170417/590785db-f7cd-465a-aba2-6661bc923ad6.jpg\\\" style=\\\"width: 700px; height: 525px;\\\" /><br />\\r\\n然后发现这张拓片有点大，随后我有添了一张小图片<br />\\r\\n<img alt=\\\"\\\" src=\\\"http://data.fanrenqiji.com/upload/20170417/f6055913-6984-4cbd-b0d3-8c9ced65b054.jpg\\\" style=\\\"width: 704px; height: 514px;\\\" /><br />\\r\\n我发现这张图也有点大，再来张小点的<img alt=\\\"\\\" src=\\\"http://data.fanrenqiji.com/upload/20170417/0dcfbfa2-f748-4ef2-919a-ab9609e18824.png\\\" style=\\\"width: 40px; height: 40px;\\\" /> 这侧够小了吧";
        String reg1="<[^>]*>";//去除所有html标签
        text = text.replaceAll(reg1, "");
        return text;
    }

}
